//
//  InfoCenterViewController.m
//  xingfugangwan
//
//  Created by rainday on 14/12/25.
//  Copyright (c) 2014年 rainday. All rights reserved.
//

#import "InfoCenterViewController.h"
#import <AFNetworking.h>
#import "OrderListViewController.h"
#import "NetIP.h"


#define LIST @[@"预约订单",@"已完成订单",@"未完成订单",@"修改密码"]
#define STATUS @[@"已预约",@"已完成",@"已签约"]

@interface InfoCenterViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,weak) IBOutlet UIImageView *aPhoto;
@property (nonatomic ,weak) IBOutlet UITableView *tableView;
@property (nonatomic ,weak) IBOutlet UIButton *logoutButton;

- (IBAction)doLogout:(id)sender;

@end
static NSString *reuserIdentify = @"Cell";

@implementation InfoCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _aPhoto.layer.cornerRadius = 70;
    _aPhoto.layer.masksToBounds = YES;
    
    _aPhoto.layer.shadowColor = [[UIColor grayColor] CGColor];
    
    _logoutButton.layer.cornerRadius = 5.0f;
    
    UIColor * color = [UIColor whiteColor];
    NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:UITextAttributeTextColor];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.title = @"业务中心";
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuserIdentify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuserIdentify];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    cell.textLabel.text = LIST[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:cell.textLabel.text];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if(indexPath.row ==3){
        return;
    }
    
    OrderListViewController *orderListVC = [[OrderListViewController alloc] initWithNibName:@"OrderListViewController" bundle:nil withStatus:STATUS[indexPath.row]];
    orderListVC.navigationItem.title = STATUS[indexPath.row];
    
    [self.navigationController pushViewController:orderListVC animated:YES];
}
- (void)doLogout:(id)sender
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    id token = [userDefaults objectForKey:@"token"];
    
   
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
   
    [manager POST:[NSString stringWithFormat:@"%s/auth/logout/",IPSTRING] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        [self performSelectorOnMainThread:@selector(sendLogoutNotice:) withObject:nil waitUntilDone:YES];
        
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}


- (void)sendLogoutNotice:(id)sender
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:nil forKey:@"token"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"logout" object:nil];
}

- (void)pushOrderListViewControllerWithStatus:(NSString *)status
{
    OrderListViewController *orderlistVC = [[OrderListViewController alloc] init];
    
    
    [self.navigationController pushViewController:orderlistVC animated:YES];
    
}

@end
